package com.tulele.vtool.wechat.service;

import com.tulele.vtool.model.SysGzh;
import com.tulele.vtool.service.SysGzhService;
import com.tulele.vtool.wechat.WechatAccountConfig;
import com.tulele.vtool.wechat.handler.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class WeixinService extends WxMpServiceImpl {

    @Autowired
    protected LogHandler logHandler;

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Autowired
    private MsgHandler msgHandler;

    @Autowired
    protected NullHandler nullHandler;

    @Autowired
    protected MenuHandler menuHandler;

    @Autowired
    private UnsubscribeHandler unsubscribeHandler;

    @Autowired
    private SubscribeHandler subscribeHandler;

    private WxMpMessageRouter router;

    @Autowired
    private SysGzhService sysGzhService;


    @PostConstruct
    public void init() {

        final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
        // 设置微信公众号的appid
        config.setAppId(wechatAccountConfig.getMpAppId());
        // 设置微信公众号的app corpSecret
        config.setSecret(wechatAccountConfig.getMpAppSecret());
        // 设置微信公众号的token
        config.setToken(wechatAccountConfig.getToken());
        // 设置消息加解密密钥
        config.setAesKey(wechatAccountConfig.getAesKey());
        super.setWxMpConfigStorage(config);

        this.refreshRouter();
    }

/*
    public void reSetConfigStorage(String gzhCode) {

        SysGzh sysGzh = sysGzhService.selectCacheLinkByGzhCode(gzhCode);

        if(sysGzh!=null) {

            final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();

            // 设置微信公众号的appid
            config.setAppId(sysGzh.getAppId());
            // 设置微信公众号的app corpSecret
            config.setSecret(sysGzh.getSecret());
            // 设置微信公众号的token
            config.setToken(sysGzh.getToken());
            // 设置消息加解密密钥
            config.setAesKey(sysGzh.getAesKey());

            super.setWxMpConfigStorage(config);
            this.refreshRouter();
        }
    }
*/
    private void refreshRouter() {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);

        // 记录所有事件的日志
        newRouter.rule().handler(this.logHandler).next();

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();

        // 点击菜单连接事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.VIEW).handler(this.menuHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler)
                .end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE).handler(this.unsubscribeHandler)
                .end();

        // 默认
        newRouter.rule().async(false).handler(this.msgHandler).end();

        this.router = newRouter;
    }

    public WxMpXmlOutMessage route(WxMpXmlMessage message) {
        try {
            return this.router.route(message);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }


}
